using System;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Globalization;
using log4net;

namespace X.Ray.Core.Framework
{
    public static class DateTimeHelper
    {
    	private static readonly ILog log = LogManager.GetLogger(typeof(DateTimeHelper));

 
        public static string GetMonthName(int monthInt)
		{
			string monthName = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames[monthInt -1];
			return monthName;
		}

        public static double GetServerGMTOffset()
        {
            return TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).TotalHours;
        }

        
        //public static double GetServerGMTOffset()
        //{
        //    Double timeOffset = Double.Parse("-5.00", CultureInfo.InvariantCulture);

        //    if (ConfigurationManager.AppSettings["GreenwichMeantimeOffset"] != null)
        //    {
        //        if (!Double.TryParse(
        //            ConfigurationManager.AppSettings["GreenwichMeantimeOffset"],
        //            NumberStyles.Any,
        //            CultureInfo.InvariantCulture,
        //            out timeOffset))
        //        {
        //            timeOffset = Double.Parse("-5.00", CultureInfo.InvariantCulture);
        //        }

        //    }

        //    return timeOffset;

        //}

        
        public static double GetPreferredGMTOffset()
        {
            Double timeOffset;

            if (ConfigurationManager.AppSettings["PreferredGreenwichMeantimeOffset"] != null)
            {
                if (!Double.TryParse(
                    ConfigurationManager.AppSettings["PreferredGreenwichMeantimeOffset"],
                    NumberStyles.Any,
                    CultureInfo.InvariantCulture,
                    out timeOffset))
                {
                    timeOffset = GetServerGMTOffset();
                }

            }
            else
            {
                timeOffset = GetServerGMTOffset();
            }

            return timeOffset;
        }


        public static String GetTimeZoneAdjustedDateTimeString(
            object objDateTime, 
            double timeZoneOffset)
        {
            String result = String.Empty;

            if (objDateTime != DBNull.Value)
            {
                try
                {
                    result = Convert.ToDateTime(objDateTime).AddHours(timeZoneOffset).ToString();
                }
                catch (InvalidCastException) 
                { }

            }

            return result;

        }

        public static String GetTimeZoneAdjustedDateTimeString(
            DbDataRecord dataRecord,
            String fieldName,
            double timeZoneOffset)
        {
            String result = String.Empty;
            

            if (
                (dataRecord != null)
                &&(fieldName != null)
                &&(dataRecord.GetOrdinal(fieldName) > -1)
                &&(dataRecord[fieldName] != DBNull.Value)
                )
            {
                try
                {
                    result = Convert.ToDateTime(dataRecord[fieldName]).AddHours(timeZoneOffset).ToString();
                }
                catch (InvalidCastException)
                { }

            }

            return result;

        }

        public static String GetTimeZoneAdjustedDateTimeString(
            DbDataRecord dataRecord,
            String fieldName,
            double timeZoneOffset,
            String formatString)
        {
            String result = String.Empty;
            
            log.Debug("GetTimeZoneAdjustedDateTimeString fieldname was " + fieldName);


            if (
                (dataRecord != null)
                && (fieldName != null)
                )
            {
                try
                {
                    if (dataRecord.GetOrdinal(fieldName) > -1)
                    {
                        if (dataRecord[fieldName] != DBNull.Value)
                        {
                            result = Convert.ToDateTime(dataRecord[fieldName]).AddHours(timeZoneOffset).ToString(formatString);

                        }
                    }
                }
                catch (InvalidCastException)
                { }

            }

            return result;

        }

        public static String GetTimeZoneAdjustedDateTimeString(
            DataRowView dataRecord,
            String fieldName,
            double timeZoneOffset)
        {
            String result = String.Empty;

            if (
                (dataRecord != null)
                && (fieldName != null)
                && (dataRecord[fieldName] != DBNull.Value)
                )
            {
                try
                {
                    result = Convert.ToDateTime(dataRecord[fieldName]).AddHours(timeZoneOffset).ToString();
                }
                catch (InvalidCastException)
                { }

            }

            return result;

        }

        public static string GetDateTimeStringForFileName()
        {
            DateTime d = DateTime.Now;
            string dateString = d.Year.ToString();

            string MonthString = d.Month.ToString();
            if (MonthString.Length == 1)
            {
                MonthString = "0" + MonthString;
            }
            string DayString = d.Day.ToString();
            if (DayString.Length == 1)
            {
                DayString = "0" + DayString;
            }
            string HourString = d.Hour.ToString();
            if (HourString.Length == 1)
            {
                HourString = "0" + HourString;
            }

            string minuteString = d.Minute.ToString();
            if (minuteString.Length == 1)
            {
                minuteString = "0" + minuteString;
            }

            string secondString = d.Second.ToString();
            if (secondString.Length == 1)
            {
                secondString = "0" + secondString;
            }
            
            dateString 
                = dateString 
                + MonthString 
                + DayString 
                + HourString 
                + minuteString + secondString;

            return dateString;
        }


    }


}
